//import gsApi from '@/api/gsApi';
//把类名设置成WebSocket会导致错误。因为名字冲突了
export default class webSocket{
    //任务状态的实时数据
    taskStatus={
        //这两个属性给出初始值，好让前端页面一开始有东西显示
        statusCode:'无',
        statusMsg:'空'
    };
    //任务状态的WebSocket的实例化对象
    taskWS;
    taskPosition;
    //导航状态的实时数据
    navigationStatus={
        realDestination:{
            angle:'',
            gridPosition:{
                x:'',
                y:''
            },
            name:''
        },
        noticeDataFields:'',
        noticeType:'',
        noticeTypeLevel:'',
    };
    //把api中英文的提示翻译为中文表达存入这个属性，更加界面友好
    navigationNotic="未在执行导航任务";
    //任务状态的WebSocket的实例化对象
    navigationWS;
    //设备状态的实时数据
    deviceStatus = {
        battery:"webSocket未启动",
        speed:0
    };
    //设备状态的WebSocket的实例化对象
    deviceStatusWS;
    
    /**
     * 空构造函数
     */
    constructor(){

    }
    testSocket(){
        let ws = new WebSocket('ws://10.7.5.88:8089/gs-robot/notice/navigation_status');
        ws.onopen = function() {
          
        };
        ws.addEventListener('message', function (event) {
           console.log(event.data)
        });
    }

    taskStatus_WebSocket(){
        try{
            this.taskWS = new WebSocket('ws://10.7.5.88:8089/gs-robot/notice/status');
        }catch(err){
            alert('任务状态的WebSocket创建失败，详见控制台')
            console.log(err);
        }
        this.taskWS.onopen = function() {
            
        };
        this.taskWS.onerror = function(){
            
        }
        this.taskWS.onclose = function(){
         
        }
        // let self = this;
        // this.ws.addEventListener('message', function(event){
        //     console.log('设备状态',event.data);
        //     let dataJson = JSON.parse(event.data);
        //     console.log(dataJson);
        //     self.currentStatus = dataJson.statusCode;
        //     console.log(self.currentStatus);
        // })
        this.taskWS.addEventListener('message',(event)=>{
            let dataJson = JSON.parse(event.data);
            this.taskStatus = dataJson;
            switch(this.taskStatus.statusCode){
                //把常用的几个做了翻译
                case 100:
                    this.taskStatus.statusMsg = '暂停中';break;
                case 301:
                    this.taskStatus.statusMsg = '导航去路径开始点';break;
                case 302:
                    this.taskStatus.statusMsg = '跟随路线中';break;  
                case 303:
                    this.taskStatus.statusMsg = '避障等待中';break;   
                case 304:
                    this.taskStatus.statusMsg = '避障中';break;
                case 305:
                    this.taskStatus.statusMsg = '路径不安全，无法到达';break;
                case 306:
                    this.taskStatus.statusMsg = '路径结束';break;  

                case 401:
                    this.taskStatus.statusMsg = '定位异常';break; 
                case 402:
                    this.taskStatus.statusMsg = '目标点不安全';break;
                case 403:
                    this.taskStatus.statusMsg = '前方有障碍物';break;
                case 404:
                    this.taskStatus.statusMsg = '目标点不可达';break;    
                case 405:
                    this.taskStatus.statusMsg = '导航行走中';break;
                case 406:
                    this.taskStatus.statusMsg = '规划路径中';break;
                case 407:
                    this.taskStatus.statusMsg = '到达目标点';break;  
                case 408:
                    this.taskStatus.statusMsg = '未到达目标点';break;

                case 600:
                    this.taskStatus.statusMsg = '正在转圈';break;
                case 601:
                    this.taskStatus.statusMsg = '不能转圈';break;
                case 602:
                    this.taskStatus.statusMsg = '转圈结束';break;
                case 603:
                    this.taskStatus.statusMsg = '转圈失败';break;
                case 701:
                    this.taskStatus.statusMsg = '机器人附近有障碍物（激光数据）';break;
                case 804:
                    this.taskStatus.statusMsg = '任务停止';break;
                case 805:
                    this.taskStatus.statusMsg = '任务完成';break;
                case 1004:
                    this.taskStatus.statusMsg = '定位程序异常，需重启';break;
                case 1006:
                    this.taskStatus.statusMsg = '定位丢失';break;
            }
        })
        
    }
    navigation_WebSocket(){
        try{
            this.navigationWS = new WebSocket('ws://10.7.5.88:8089/gs-robot/notice/navigation_status');
        }catch(err){
            alert('导航状态的WebSocket创建失败，详见控制台')
            console.log(err)
        }
        this.navigationWS.addEventListener('message',(event)=>{
            let dataJson = JSON.parse(event.data);
            // this.navigationStatus = dataJson.data;
            switch(dataJson.noticeType){
                case "REACHED":
                    this.navigationNotic = "到达目的地";break;
                case "HEADING":
                    this.navigationNotic = "正在移动中";break;
                case "PLANNING":
                    this.navigationNotic = "正在规划路径";break;
                case "UNREACHED":
                    this.navigationNotic = "到达目的地附近，目的地有障碍物";break;
                case "UNREACHABLE":
                    this.navigationNotic = "目的地无法到达";break;
                case "LOCALIZATION_FAILED":
                    this.navigationNotic = "定位失败";break;
                case "TOO_CLOSE_TO_OBSTACLES":
                    this.navigationNotic = "离障碍物太近";break;
                case "GOAL_NOT_SAFE":
                    this.navigationNotic = "目的地有障碍物";break;
            }
           
            // this.status.statusCode = dataJson.statusCode;
            // this.status.msg = dataJson.msg;
        })
    }
    device_WebSocket(){
        try{
            this.deviceStatusWS = new WebSocket('ws://10.7.5.88:8089/gs-robot/notice/device_status');
        }catch(err){
            alert('设备状态的WebSocket创建失败，详见控制台')
            console.log(err)
        }
        this.deviceStatusWS.addEventListener('message',(event)=>{
            let dataJson = JSON.parse(event.data);
            dataJson.speed = dataJson.speed.toFixed(2);
            this.deviceStatus = dataJson;
            
         })
    }

    stopWebSocket(){
        this.taskWS.close();
        this.navigationWS.close();
        this.deviceStatusWS.close();
        this.deviceStatus.battery="webSocket未启动";
    }
    startWebSocket(){
        console.log('启动webScoket')
        try {
            this.taskStatus_WebSocket();
            this.navigation_WebSocket();
            this.device_WebSocket();
        }catch (err) {
            console.log(err);
            this.$message.error('webSocket无法启动')
        }
    }
}